上次成功加入GET
跟POST
,這次我們要建立一個通用的Models。
有時我們會遇到重複使用新增、修改、刪除的變數,這時我們需要一個通用的功能來達成目的,首先我們先在Models
新增Mod_universal.php
檔案:
<?php defined('BASEPATH') or exit('No direct script access allowed');
class Mod_universal extends CI_Model {
public function __construct() {
parent::__construct();
}
}
/* End of file Mod_universal.php */
接著我們透過指令新增功能,參考文件可以尋找libraries/Mongo_db.php
。
/**
* 確認資料是否存在
* verify = 驗證
* likes = 相似關鍵字
* table = 資料庫名稱 (必要)
*/
function chk_once($dataQuery){
if (isset($dataQuery['verify'])) $this->mongo_db->where($dataQuery['verify']);
if (isset($dataQuery['likes'])) {
foreach ($dataQuery['likes'] as $key => $value) {
$this->mongo_db->like($key, $value);
}
}
$response = $this->mongo_db->count($dataQuery['table']);
if ($response == 0) {
return false;
} else {
return true;
}
}
/**
* 取得資料數量
* verify = 驗證
* likes = 相似關鍵字
* table = 資料庫名稱 (必要)
* record = 特殊資料
*/
function get_count($dataQuery){
if (isset($dataQuery['verify'])) $this->mongo_db->where($dataQuery['verify']);
if (isset($dataQuery['likes'])) {
foreach ($dataQuery['likes'] as $key => $value) {
$this->mongo_db->like($key, $value);
}
}
if (isset($dataQuery['record']['between'])) {
$this->mongo_db->where_between($dataQuery['record']['between']['item'],
$dataQuery['record']['between']['start'], $dataQuery['record']['between']['end']);
}
if (isset($dataQuery['record']['where_in'])) {
$this->mongo_db->where_in($dataQuery['record']['where_in']['key'], $dataQuery['record']['where_in']['value']);
}
if (isset($dataQuery['record']['or_where'])) {
$this->mongo_db->where_or($dataQuery['record']['or_where']);
}
if (isset($dataQuery['record']['where_gte'])) {
$this->mongo_db->where_gte($dataQuery['record']['where_gte']['key'], $dataQuery['record']['where_gte']['value']);
}
if (isset($dataQuery['record']['where_lte'])) {
$this->mongo_db->where_lte($dataQuery['record']['where_lte']['key'], $dataQuery['record']['where_lte']['value']);
}
if (isset($dataQuery['record']['order_by'])) {
$this->mongo_db->order_by($dataQuery['record']['order_by']);
} else {
$this->mongo_db->order_by(array('createAt'=> 'DESC', 'createAtTime'=> 'DESC'));
}
if (isset($dataQuery['record']['where_not_in'])) {
$this->mongo_db->where_not_in($dataQuery['record']['where_not_in']['key'], $dataQuery['record']['where_not_in']['value']);
}
$response = $this->mongo_db->count($dataQuery['table']);
return $response;
}
/**
* 取得單一資料
* verify = 驗證
* likes = 相似關鍵字
* table = 資料庫名稱 (必要)
* record = 特殊資料
*/
function get_once($dataQuery){
if (isset($dataQuery['verify'])) $this->mongo_db->where($dataQuery['verify']);
if (isset($dataQuery['likes'])) {
foreach ($dataQuery['likes'] as $key => $value) {
$this->mongo_db->like($key, $value);
}
}
if (isset($dataQuery['record']['between'])) {
$this->mongo_db->where_between($dataQuery['record']['between']['item'],
$dataQuery['record']['between']['start'], $dataQuery['record']['between']['end']);
}
if (isset($dataQuery['record']['where_in'])) {
$this->mongo_db->where_in($dataQuery['record']['where_in']['key'], $dataQuery['record']['where_in']['value']);
}
if (isset($dataQuery['record']['or_where'])) {
$this->mongo_db->where_or($dataQuery['record']['or_where']);
}
if (isset($dataQuery['record']['where_gte'])) {
$this->mongo_db->where_gte($dataQuery['record']['where_gte']['key'], $dataQuery['record']['where_gte']['value']);
}
if (isset($dataQuery['record']['where_lte'])) {
$this->mongo_db->where_lte($dataQuery['record']['where_lte']['key'], $dataQuery['record']['where_lte']['value']);
}
if (isset($dataQuery['record']['order_by'])) {
$this->mongo_db->order_by($dataQuery['record']['order_by']);
} else {
$this->mongo_db->order_by(array('createAt'=> 'DESC', 'createAtTime'=> 'DESC'));
}
if (isset($dataQuery['record']['where_not_in'])) {
$this->mongo_db->where_not_in($dataQuery['record']['where_not_in']['key'], $dataQuery['record']['where_not_in']['value']);
}
$response = $this->mongo_db->find_one($dataQuery['table']);
return $response;
}
/**
* 取得特定資料
* verify = 驗證
* likes = 相似關鍵字
* table = 資料庫名稱 (必要)
* record = 特殊資料
*/
function get_list($dataQuery){
if (isset($dataQuery['verify'])) $this->mongo_db->where($dataQuery['verify']);
if (isset($dataQuery['likes'])) {
foreach ($dataQuery['likes'] as $key => $value) {
$this->mongo_db->like($key, $value);
}
}
if (isset($dataQuery['record']['limit'])) $this->mongo_db->limit($dataQuery['record']['limit']);
if (isset($dataQuery['record']['page'])) $this->mongo_db->offset($dataQuery['record']['limit']*($dataQuery['record']['page'] - 1));
if (isset($dataQuery['record']['between'])) {
$this->mongo_db->where_between($dataQuery['record']['between']['item'],
$dataQuery['record']['between']['start'], $dataQuery['record']['between']['end']);
}
if (isset($dataQuery['record']['where_in'])) {
$this->mongo_db->where_in($dataQuery['record']['where_in']['key'], $dataQuery['record']['where_in']['value']);
}
if (isset($dataQuery['record']['or_where'])) {
$this->mongo_db->where_or($dataQuery['record']['or_where']);
}
if (isset($dataQuery['record']['where_gte'])) {
$this->mongo_db->where_gte($dataQuery['record']['where_gte']['key'], $dataQuery['record']['where_gte']['value']);
}
if (isset($dataQuery['record']['where_lte'])) {
$this->mongo_db->where_lte($dataQuery['record']['where_lte']['key'], $dataQuery['record']['where_lte']['value']);
}
if (isset($dataQuery['record']['order_by'])) {
$this->mongo_db->order_by($dataQuery['record']['order_by']);
} else {
$this->mongo_db->order_by(array('createAt'=> 'DESC', 'createAtTime'=> 'DESC'));
}
if (isset($dataQuery['record']['where_not_in'])) {
$this->mongo_db->where_not_in($dataQuery['record']['where_not_in']['key'], $dataQuery['record']['where_not_in']['value']);
}
$response = $this->mongo_db->get($dataQuery['table']);
return $response;
}
/**
* 取得所有特定資料
* verify = 驗證
* likes = 相似關鍵字
* table = 資料庫名稱 (必要)
* record = 特殊資料
*/
function get_all($dataQuery){
if (isset($dataQuery['verify'])) $this->mongo_db->where($dataQuery['verify']);
if (isset($dataQuery['likes'])) {
foreach ($dataQuery['likes'] as $key => $value) {
$this->mongo_db->like($key, $value);
}
}
if (isset($dataQuery['record']['between'])) {
$this->mongo_db->where_between($dataQuery['record']['between']['item'],
$dataQuery['record']['between']['start'], $dataQuery['record']['between']['end']);
}
if (isset($dataQuery['record']['where_in'])) {
$this->mongo_db->where_in($dataQuery['record']['where_in']['key'], $dataQuery['record']['where_in']['value']);
}
if (isset($dataQuery['record']['or_where'])) {
$this->mongo_db->where_or($dataQuery['record']['or_where']);
}
if (isset($dataQuery['record']['where_gte'])) {
$this->mongo_db->where_gte($dataQuery['record']['where_gte']['key'], $dataQuery['record']['where_gte']['value']);
}
if (isset($dataQuery['record']['where_lte'])) {
$this->mongo_db->where_lte($dataQuery['record']['where_lte']['key'], $dataQuery['record']['where_lte']['value']);
}
if (isset($dataQuery['record']['order_by'])) {
$this->mongo_db->order_by($dataQuery['record']['order_by']);
} else {
$this->mongo_db->order_by(array('createAt'=> 'DESC', 'createAtTime'=> 'DESC'));
}
if (isset($dataQuery['record']['where_not_in'])) {
$this->mongo_db->where_not_in($dataQuery['record']['where_not_in']['key'], $dataQuery['record']['where_not_in']['value']);
}
$response = $this->mongo_db->get($dataQuery['table']);
return $response;
}
基本上透過傳入值來新增內容,其中裡面的table
代表特定資料庫是必要的。
今天我們就到這,明天接下去寫新增、刪除、修改。
Next station ... 通用Models